package com.example.chapter04;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.chapter04.util.DateUtil;

public class ActResponseActivity extends AppCompatActivity implements View.OnClickListener {
    private final String mResponse = "你好，我在的，有什么事？";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_response);
        TextView tv_get_request = findViewById(R.id.tv_get_request);
        // 获取请求消息，显示到界面上
        Bundle bundle = getIntent().getExtras();
        String request_content = bundle.getString("request_content");
        String request_time = bundle.getString("request_time", DateUtil.getNowDateTime());
        String desc = String.format("收到请求消息：%s\n请求时间：%s", request_content, request_time);
        tv_get_request.setText(desc);

        // 设置返回按钮的点击事件监听器
        findViewById(R.id.btn_back_response).setOnClickListener(this);

        // 显示待返回的响应消息
        TextView tv_back_response = findViewById(R.id.tv_back_response);
        tv_back_response.setText("待返回的响应消息：" + mResponse);

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("response_time", DateUtil.getNowDateTime());
        bundle.putString("response_content", mResponse);
        intent.putExtras(bundle);
        // 携带数据返回上一个Activity
        setResult(RESULT_OK, intent);
        finish();
    }
}